package casino;

import java.util.concurrent.CopyOnWriteArrayList;

import containers.Build;
import containers.Hand;

public class HumanPlayer extends Player {
	private Build forcedBuild = new Build(casino, 0, 0);

	public HumanPlayer(Casino n){
		super(n);
	}
	@Override
	public void update() {
		if(casino.sCard != null){
			if(adding){
				Build b = new Build(casino);
				if(casino.builds.add(b)){
					if(casino.sCard.parent!=null)casino.sCard.parent.remove(casino.sCard);
					b.add(casino.sCard);
					adding = false;
					casino.sCard = null;
					endTurn();
					return;
				}
				adding = false;
				casino.sCard = null;
				choosing = true;
			}else if(casino.sBuild != null){
				if(taking){
					if(casino.sBuild.validTake(casino.sCard)){
						if(!casino.isBuild(forcedBuild) || forcedBuild.equals(casino.sBuild)){
							if(casino.sCard.parent!=null)casino.sCard.parent.remove(casino.sCard);
							casino.sBuild.add(casino.sCard);
							discard.addAll(casino.sBuild.flush());
							casino.builds.remove(casino.sBuild);
							forcedBuild = null;
							endTurn();
						}else{
							casino.addMessage("Cannot take that build");
							choosing = true;
						}
					}else{
						casino.addMessage("Incorrect Value");
						choosing = true;
					}
					taking = false;
				}else if(building){
					if(!casino.isBuild(forcedBuild) || forcedBuild.equals(casino.sBuild)){
						if(canAdd(casino.sBuild, casino.sCard)){
							if(casino.sCard.parent!=null)casino.sCard.parent.remove(casino.sCard);
							casino.sBuild.add(casino.sCard);
							if(!casino.isBuild(forcedBuild)){
								forcedBuild = casino.sBuild;
							}
							endTurn();
						}else{
							casino.addMessage("Cannot Increase that build");
							choosing = true;
						}
					}else{
						casino.addMessage("Cannot increase that build");
						choosing = true;
					}
					building = false;
				}else if(duplicating){
					if(casino.sBuild.validTake(casino.sCard)){
						if(!casino.isBuild(forcedBuild) || forcedBuild.equals(casino.sBuild)){
							if(casino.sCard.parent!=null)casino.sCard.parent.remove(casino.sCard);
							casino.sBuild.addDuplicate(casino.sCard);
							if(!casino.isBuild(forcedBuild)){
								forcedBuild = casino.sBuild;
							}
							endTurn();
						}else{
							casino.addMessage("Cannot duplicate that build");
							choosing = true;
						}
					}else{
						casino.addMessage("Incorrect value");
						choosing = true;
					}
					duplicating = false;
				}
				casino.sCard = null;
				casino.sBuild = null;
			}
		}
	}
	
	private boolean canAdd(Build b, GameCard c){
		int val = b.value()+c.getValue();
		if(val > 14){
			return false;
		}
		CopyOnWriteArrayList<GameCard> t = new CopyOnWriteArrayList<GameCard>();
		t.addAll(hand.cards);
		t.remove(c);
		return testValue(t, val);
	}
	
	private boolean testValue(CopyOnWriteArrayList<GameCard> h, int val){
		System.out.println(h.size());
		for(GameCard c: h){
			if(c.getValue()+val > 14){
				continue;
			}else if(hand.containsValue(val+c.getValue())){
				return true;
			}else{
				CopyOnWriteArrayList<GameCard> t = new CopyOnWriteArrayList<GameCard>();
				t.addAll(h);
				t.remove(c);
				if(testValue(t, val+c.getValue())){
					return true;
				}
			}
		}
		return false;
	}

}
